package info

import (
	"encoding/json"
	"time"

	"github.com/yusufpapurcu/wmi"
)

type Win32_SoundDevice struct {
	Availability                uint16 `json:"可用性"`
	Caption                     string `json:"说明"`
	ConfigManagerErrorCode      uint32 `json:"错误代码"`
	ConfigManagerUserConfig     bool   `json:"是否用户定义的配置"`
	CreationClassName           string
	Description                 string    `json:"描述"`
	DeviceID                    string    `json:"声音设备的识符"`
	DMABufferSize               uint16    `json:"直接内存访问缓冲区的大小"`
	ErrorCleared                bool      `json:"清除错误报告"`
	ErrorDescription            string    `json:"错误的详细信息"`
	InstallDate                 time.Time `json:"安装时间"`
	LastErrorCode               uint32    `json:"最后一个错误代码"`
	Manufacturer                string    `json:"制造商"`
	MPU401Address               uint32    `json:"分配给声音设备的MPU-401端口的I/O地址"`
	Name                        string    `json:"名称"`
	PNPDeviceID                 string    `json:"逻辑设备的设备标识符"`
	PowerManagementCapabilities []uint16  `json:"定电源相关功"`
	PowerManagementSupported    bool      `json:"电源管理"`
	ProductName                 string    `json:"产品名称"`
	Status                      string    `json:"状态"`
	StatusInfo                  uint16    `json:"逻辑设备状态"`
	SystemCreationClassName     string
	SystemName                  string
}

func SoundInfo() (sound []Win32_SoundDevice, err error) {
	err1 := wmi.Query("Select * from Win32_SoundDevice", &sound)
	if err1 != nil {
		return nil, err1
	}

	return sound, nil
}

func JsonSoundInfo() (t []byte, err error) {
	sound, err := SoundInfo()
	if err != nil {
		return t, err
	}
	t, jsonerr := json.Marshal(sound)
	if jsonerr != nil {
		return t, jsonerr
	}
	return t, nil
}

func MapJsonSoundInfo() (data []map[string]interface{}, err error) {

	bus, err := JsonSoundInfo()
	if err != nil {
		return data, err
	}
	json.Unmarshal(bus, &data)
	return data, nil
}
